<template>
	<view class="posts_all">
		<view class="top">
			<view v-if="!iconFlag"><u-icon name="star-fill" color="#753eee" :label="postsAllTitle"></u-icon></view>
			<view v-else class="custom-icon custom-icon-biaotiqian">{{postsAllTitle}}</view>
			<text v-show="flag">已点亮({{count}}/{{total}})</text>
		</view>
		<view class="allFound_list">
			<view v-for="(item, index) in list" :key="index" class="viewList" @click="setActive(item.id)">
				<u-avatar class="img" :src="item.cover"></u-avatar>
				<view :class="['name', { 'active': postsActiveIndex === item.id }]">
					{{ item.name }}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "Classify",
		props: {
			list: {
				type: Array,
				default: () => []
			},
			postsAllTitle: {
				type: String,
				default: '' // 默认值
			},
			flag: {
				type: Boolean,
				default: false
			},
			iconFlag: {
				type: Boolean,
				default: false
			},
			total: {
				type: Number,
				default: 0
			},
			count: {
				type: Number,
				default: 0
			},
			childIndex: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				postsActiveIndex: this.childIndex, // 初始化选中的索引
			};
		},
		watch: {
			childIndex(newVal) {
				this.postsActiveIndex = newVal;
			}
		},
		methods: {
			setActive(index) {
				console.log("this.childIndex", this.childIndex)
				this.postsActiveIndex = index; // 设置选中的索引
				console.log(index)
				this.$emit("Child", index)
			}
		}
	}
</script>

<style scoped lang="scss">
	.top {
		display: flex;
		justify-content: space-between;
		align-items: center;
		line-height: 100rpx;
		
		margin:0 20rpx;

		text {
			font-size: 24rpx;
			color: #aeaeae;
		}
	}

	.allFound_list {
		display: flex;
		flex-wrap: wrap;
		// justify-content: space-between;

		.viewList {
			width: 25%;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding: 20rpx 0;
			cursor: pointer; // 添加悬停效果

			&:hover {
				background-color: #f9f9f9; // 悬停高亮效果
			}

			.img {
				margin-bottom: 10rpx; // 添加图片和文本之间的间距
			}

			.name {
				font-size: 24rpx;
				color: #000;

				&.active {
					color: #f3741b; // 使用类来控制颜色
				}
			}
		}
	}
</style>